package dictcn.com.model;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	private Context context;
	private ImageView imgIds[];
	private int selectedImg;
	
	public ImageAdapter(Context context,int imgIds[],int width,int height,int selectedImg){
		
		this.context=context;
		this.selectedImg=selectedImg;
		this.imgIds=new ImageView[imgIds.length];
		for(int x=0;x<imgIds.length;x++){
			this.imgIds[x]=new ImageView(this.context);
			this.imgIds[x].setLayoutParams(new GridView.LayoutParams(width,height));
			this.imgIds[x].setAdjustViewBounds(false);
			this.imgIds[x].setPadding(5, 5, 5, 5);
			this.imgIds[x].setImageResource(imgIds[x]);
		}
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return this.imgIds.length;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.imgIds[position];
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView imgView=null;
		if(convertView==null){
			imgView=this.imgIds[position];
		}else{
			imgView=(ImageView)convertView;
		}
		return imgView;
	}
	
	public void setFocus(int selectedId){
		for(int x=0;x<this.imgIds.length;x++){
			if(x!=selectedId){
				this.imgIds[x].setBackgroundResource(0);
			}
		}
		this.imgIds[selectedId].setBackgroundResource(this.selectedImg);
	}
}
